library(tidyverse)
library(ggrastr)
library(ggrepel)
library(patchwork)

load("data/plotting_data.RData")

grid_data <- url_df |> 
    select(domain, clean_url, num_domain_urls, contains("score")) |> 
    pivot_longer(shares_score:domain_clicks_score) |>
    mutate(type = if_else(str_detect(name, "domain"), "Domain", "URL"),
           action = as.factor(str_to_title(str_extract(name, "(?:domain_)?([a-z]+)_score", group=1))))

make_grid_plot <- function (gd, threshold) {
    gd <- filter(gd, num_domain_urls > threshold)
    
    grid_plot <-  ggplot(data = NULL, aes(x=value, color = type, linetype = type)) +
    geom_density(data= distinct(filter(gd, type == "Domain"), domain, num_domain_urls, type, action, value), linewidth = 2) + 
    geom_density(data=filter(gd, type == "URL"), linewidth = 2) + 
    facet_wrap(vars(action), nrow=2) +
    coord_cartesian(xlim = c(-1, 1)) +
    scale_linetype_manual(values = c(1, 6)) +
    scale_color_manual(values = c("#984ea3", "#ff7f00")) +
    theme_classic()+
    theme(text = element_text(family = "serif", size = 12),
         plot.title = element_text(size = 16, face = "bold"),
         legend.position = "bottom",
         legend.direction = "horizontal",
         panel.grid.major.y = element_line(color="grey90"),
        panel.grid.minor.y = element_line(color="grey96")) +
    labs(x = "Audience Score", 
         y = "Density",
         color = "Type",
         linetype = 'Type',
        )
    }

grid_plot <- make_grid_plot(grid_data,
                            quantile(domains$num_domain_urls, 0.75))
ggsave(grid_plot, file = "results/fig_5.pdf", height = 6, width = 7, dpi = 300)
ggsave(grid_plot, file = "results/grid_plot.pdf", height = 6, width = 7, dpi = 300)
grid_plot <- make_grid_plot(grid_data,
                            quantile(domains$num_domain_urls, 0)) #+ 
ggsave(grid_plot, file = "results/fig_i4.pdf", height = 6, width = 7, dpi = 300)
ggsave(grid_plot, file = "results/grid_plot_no_thresh.pdf", height = 6, width = 7, dpi = 300)
grid_plot <- make_grid_plot(grid_data,
                            quantile(domains$num_domain_urls, 0.5)) #+ 
ggsave(grid_plot, file = "results/fig_i5.pdf", height = 6, width = 7, dpi = 300)
ggsave(grid_plot, file = "results/grid_plot_median_thresh.pdf", height = 6, width = 7, dpi = 300)
grid_plot <- make_grid_plot(grid_data,
                            quantile(domains$num_domain_urls, 0.9)) #+ 
ggsave(grid_plot, file = "results/fig_i6.pdf", height = 6, width = 7, dpi = 300)
ggsave(grid_plot, file = "results/grid_plot_high_thresh.pdf", height = 6, width = 7, dpi = 300)

